<?php
echo '<script type="text/javascript" src="'.base_url().'lib/jquery-1.9.0.js"></script>';
echo $ckeditor;
function find_parent($cat,$parent_id)
{
    if($parent_id == 0)
        return true;
    foreach($cat as $v)
    {
        if($v->id == $parent_id)
        {   
          return true;
        }
    }
    return false;
}
function recu($cat,$parent_id,$lv)
{
    foreach($cat as $v)
    {
        $s = $parent_id;
        $x = $lv;
        if(($v->parent_id == $parent_id) || !find_parent($cat,$v->parent_id))
        {
            $lv++;
            $i = 0;
            $str = '';
            while($i < $lv)
            {
                if(!$str)
                    $str = '|_';
                $str = '&nbsp&nbsp&nbsp'.$str;
                $i++;
            }
            echo '<option value="'.$v->id.'">'.$str.$v->title.'</option>';
            $parent_id = $v->id;
            recu($cat,$parent_id,$lv);
        }
        $lv = $x;
        $parent_id = $s;
        //echo "<br />";
    }
}

?>
<style type="text/css">
.nlab{
    height: 10px;
    font-size:10px;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
  $("#submit").click(function(){
    var frm = $("#postpage");
    var data_string = frm.serialize();
    var content = encodeURIComponent(CKEDITOR.instances.content.getData());
    var intro = encodeURIComponent(CKEDITOR.instances.intro.getData());    
    data_string +='&content='+content+'&intro='+intro; 
    $.ajax({
    type: "POST",
    url: frm.attr('action'),
    data: data_string,
    dataType: "html",
    success: function(data){
        $("#result").html(data);
    }
    });
  })
});
</script>
<div style="position: fixed; float: right; right: 5px; top: 40px;">
<form id="postpage" style="width: 400px;" method="post" action="<?php echo base_url().'news/postnews'?>">
<div id="result"></div>
<div style="float: left;">
    <label class="nlab" for="title"><strong>Title news</strong></label>
    <input style="width: 240px; height: 13px;" type="text" name="title" id="title"/>
</div>
<div style="float: right;">
    <label class="nlab" for="category_id"><strong>Category</strong></label>
    <select name="category_id" id="category_id" style="width: 150px;height: 18px;">
    <option value="0">.....</option>
    <?php    
    recu($category,0,-1);
    ?>
    </select>
</div>
<div style="clear: both;"></div>

    <label class="nlab" for="intro"><strong>Intro news</strong></label>
    <textarea class="ckeditor" name="intro" id="intro"></textarea>
    <label class="nlab" for="content"><strong>content news</strong></label>
    <textarea class="ckeditor" name="content" id="content"></textarea>
<input type="hidden" name="id" id="id" />
<input type="text" name="source" id="source" class="nlab" value="<?php if(!empty($source)) echo $source;?>" /><input style="float: right;" type="button" name="submit" id="submit" value="postnews" />
</form>
</div>
<?php if(!empty($item)):?>
<script type="text/javascript">
$("#id").val("<?php echo $item->id;?>");
$("#title").val("<?php echo $item->title;?>");
$("#intro").val("<?php echo $item->title;?>");
$("#content").val("<?php echo $item->title;?>");
$("#category_id option[value=<?php echo $item->category_id;?>]").attr("selected","selected");
</script>
<?php endif;?>